articles

Home / DeveloperSection / Articles / Show Data using Dapper ORM Framework in C#

Show Data using Dapper ORM Framework in C#

Sushant Mishra3742 07-Mar-2017

Dapper is an ORM (Object Relational Mapper) of .Net Framework. ORM is a programming technique which converts data between incompatible type system in object oriented programming language. ORM creates a virtual object database that can be used within your program. Microsoft provides a product of ORM framework which is Dapper Framework.

Create Database:
CREATE TABLE [dbo].[Contacts](
          [Id] [int] IDENTITY(1,1) NOT NULL,
          [FirstName] [varchar](50) NULL,
          [Lastname] [varchar](50) NULL,
          [Email] [varchar](50) NULL,
          [Company] [varchar](50) NULL,
          [Title] [varchar](50) NULL,
 CONSTRAINT [PK_Contacts] PRIMARY KEY CLUSTERED


After creating database, we create a new console application in Visual Studio


DapperExample.



Add an Application Configuration File in your project.

App.config

<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <addconnectionString="Server=MINDSTICKSERVER\MSSQLSERVER2014;Database=ContactDB;User Id=UserName; Password=Password;"name="DefaultConnectionString"providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>


Add a Class Contacts


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DapperExample
{
    classContacts
    {
        publicstring FirstName { get; set; }
        publicstring LastName { get; set; }
        publicstring Email { get; set; }
        publicstring Company { get; set; }
        publicstring Title { get; set; }
    }
}


Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
namespace DapperExample
{
    classProgram
    {
       
 
        staticvoid Main(string[] args)
        {
            var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
            Contacts x = newContacts();
            SqlConnection db = newSqlConnection(connectionString);
            string SqlString = "SELECT [FirstName],[LastName],[Email],[Company],[Title]FROM [Contacts]";
            var ourContacts = (List<Contacts>)db.Query<Contacts>(SqlString);
            foreach (var rec in ourContacts)
            {
                Console.WriteLine(newstring('*', 20));
                Console.WriteLine("First Name: " + rec.FirstName);
                Console.WriteLine("Last Name: " + rec.LastName);
                Console.WriteLine("Email " + rec.Email );
                Console.WriteLine("Company "+rec.Company);
                Console.WriteLine("Title " + rec.Title);
                Console.WriteLine(newstring('*', 20));
            }
            Console.Read();
        }
    }
}


Updated 29-Nov-2017

Leave Comment

Comments

Liked By